package org.ee.core.util.serialize;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;

public class JsonUtil {
	/**
	 * 格式化Date,为yyyy-MM-dd HH:mm:ss格式
	 */
	public final static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * 获取预先配置好的ObjectMapper
	 * @return
	 */
	public static ObjectMapper getJsonMapper(){
		ObjectMapper om = new ObjectMapper();
		om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
		//替代WRITE_NULL_MAP_VALUES
		om.setDefaultPropertyInclusion(JsonInclude.Value.construct(Include.NON_NULL, Include.NON_NULL));
//		om.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);

		//通过该方法对mapper对象进行设置，所有序列化的对象都将按改规则进行系列化
		//Include.Include.ALWAYS 默认
		//Include.NON_DEFAULT 属性为默认值不序列化
		//Include.NON_EMPTY 属性为 " 或者为 NULL 都不序列化
		//Include.NON_NULL 属性为NULL 不序列化
		om.setSerializationInclusion(Include.NON_NULL);
		//日期修改为String格式进行传输
//		SimpleDateFormat formatter = new SimpleDateFormat(DateHelper.LongDateFormat24);
//		om.setDateFormat(formatter);
		om.setDateFormat(dateFormat);

		//反序列化时，若实体类没有对应的属性，是否抛出JsonMappingException异常，false忽略掉
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		//序列化是否绕根元素，true，则以类名为根元素
		om.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
		//XML标签名:使用骆驼命名的属性名，
		//om.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
		//设置转换模式
//        om.enable(MapperFeature.USE_STD_BEAN_NAMING);

		//设置时区
		om.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		om.setLocale(Locale.CHINA);
		return om;
	}

	/**
	 * 获取序列化海康摄像头、温测精灵的json序列化规则
	 * @return
	 */
	public static ObjectMapper getJsonCameraMapper(){
		ObjectMapper om = new ObjectMapper();
		om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
		//替代WRITE_NULL_MAP_VALUES
		om.setDefaultPropertyInclusion(JsonInclude.Value.construct(Include.NON_NULL, Include.NON_NULL));

//		om.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);

		//通过该方法对mapper对象进行设置，所有序列化的对象都将按改规则进行系列化
		//Include.Include.ALWAYS 默认
		//Include.NON_DEFAULT 属性为默认值不序列化
		//Include.NON_EMPTY 属性为 " 或者为 NULL 都不序列化
		//Include.NON_NULL 属性为NULL 不序列化
		om.setSerializationInclusion(Include.NON_NULL);
		//日期修改为String格式进行传输
//		SimpleDateFormat formatter = new SimpleDateFormat(DateHelper.LongDateFormat24);
////		om.setDateFormat(formatter);
//		om.setDateFormat(CommonUtil.mfAll);

		//反序列化时，若实体类没有对应的属性，是否抛出JsonMappingException异常，false忽略掉
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

		om.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);

		om.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		om.setLocale(Locale.CHINA);
		return om;
	}

	/**
	 * 获取预先配置好的ObjectMapper
	 * @return
	 */
	public static ObjectMapper getJsonFullMapper(){
		ObjectMapper om = new ObjectMapper();
		om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
		//替代WRITE_NULL_MAP_VALUES
		om.setDefaultPropertyInclusion(JsonInclude.Value.construct(Include.NON_NULL, Include.NON_NULL));
//		om.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
		om.setSerializationInclusion(Include.ALWAYS);
		//日期修改为String格式进行传输
//		SimpleDateFormat formatter = new SimpleDateFormat(DateHelper.LongDateFormat24);
//		om.setDateFormat(formatter);
		om.setDateFormat(dateFormat);

		//反序列化时，若实体类没有对应的属性，是否抛出JsonMappingException异常，false忽略掉
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		//序列化是否绕根元素，true，则以类名为根元素
		om.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
		//XML标签名:使用骆驼命名的属性名，
		//om.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
		//设置转换模式
//        om.enable(MapperFeature.USE_STD_BEAN_NAMING);

		//设置时区
		om.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		om.setLocale(Locale.CHINA);
		return om;
	}
	/**
	 * Log库Json ObjectMapper
	 * @return
	 */
	public static ObjectMapper getJsonLogMapper(){
		ObjectMapper om = new ObjectMapper();
		om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

		//替代WRITE_NULL_MAP_VALUES
		om.setDefaultPropertyInclusion(JsonInclude.Value.construct(Include.NON_EMPTY, Include.NON_EMPTY));
//		om.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
		//通过该方法对mapper对象进行设置，所有序列化的对象都将按改规则进行系列化
		//Include.Include.ALWAYS 默认
		//Include.NON_DEFAULT 属性为默认值不序列化
		//Include.NON_EMPTY 属性为 " 或者为 NULL 都不序列化
		//Include.NON_NULL 属性为NULL 不序列化
		om.setSerializationInclusion(Include.NON_EMPTY);
		//日期修改为String格式进行传输
//		SimpleDateFormat formatter = new SimpleDateFormat(DateHelper.LongDateFormat24);
//		om.setDateFormat(formatter);
		om.setDateFormat(dateFormat);

		//反序列化时，若实体类没有对应的属性，是否抛出JsonMappingException异常，false忽略掉
		om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
		//序列化是否绕根元素，true，则以类名为根元素
		om.configure(SerializationFeature.WRAP_ROOT_VALUE, false);
		//XML标签名:使用骆驼命名的属性名，
		//om.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
		//设置转换模式
//        om.enable(MapperFeature.USE_STD_BEAN_NAMING);

		//设置时区
		om.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
		om.setLocale(Locale.CHINA);
		return om;
	}
}
